package test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;

/**
 * @author liangQx
 * @date build time 2016年1月20日 下午3:12:18
 */
public class Save {

	public Trie root = new Trie();
	public char[] ss = new char[100];

	public void BuildTrie(String str) {
		char s[] = new char[100];
		s = str.toCharArray();
		int i, j, len = str.length();
		Trie t = root;
		for (i = 0; i < len; i++) {
			for (j = 0; j < t.leng; j++) {
				if (t.next.get(j).ch == s[i])
					break;
			}
			if (j < t.leng)
				t = t.next.get(j);
			else {
				Trie h = new Trie();
				h.ch = s[i];
				h.flag = 0;
				if (t.leng == 0)
					t.next = new LinkedList<Trie>();
				t.next.add(h);
				t.leng++;
				t = h;
				t.leng = 0;
			}
		}
		t.flag = 1;
	};

	public void Show(Trie t, int index) {
		int i;
		for (i = 0; i < t.leng; i++) {
			ss[index] = t.next.get(i).ch;
			if (t.next.get(i).flag == 1) {
				System.out.println(new String(ss, 0, index + 1));
			}
			Show(t.next.get(i), index + 1);
		}
		
		
	}

	public void find(String str){
		
		char c[] = str.toCharArray();
		
	}
	public void set(String name) {
		root.leng = 0;
		try {
			FileReader one = new FileReader(name);
			BufferedReader to = new BufferedReader(one);
			String s = to.readLine();
			while (s != null) {
				BuildTrie(s);
				s = to.readLine();
			}
			Show(root, 0);
		} catch (Exception e) {
			
		}
		;
	}
}
